#!/bin/sh
#########################################################################
##
## This file is part of the SAMRAI distribution.  For full copyright 
## information, see COPYRIGHT and LICENSE. 
##
## Copyright:     (c) 1997-2019 Lawrence Livermore National Security, LLC
## Description:   shell script to generate dependencies for SAMRAI files
##
#########################################################################

##
## Generate dependency information for all files in the current directory
## and subdirectories.
##

BIN=`dirname $0`
bindir=`cd $BIN && pwd`

PERL=${PERL:-perl}
COMP="$PERL $BIN/cmp.pl"

DIRS=`find . -type d -print | grep -v '\(config\|OLD\|NEW\|BAK\|SAVE\|to_be_implemented\|noprefix\|CVS\|\.svn\|\.git\|\.hg\)'`
for DIR in $DIRS ; do

   #
   # Generate a dependency file in the subdirectory if *.C files exist
   #

   echo "Checking dependencies in directory "$DIR
   SRCFILES=`(cd $DIR; echo *.[fCc])`
   M4FILES=`(cd $DIR; echo *.m4)`
   FILES=""
   if [ "*.[fCc]" != "$SRCFILES" ] ; then
      FILES=$SRCFILES
   fi
   if [ "*.m4" != "$M4FILES" ] ; then
      FILES="$FILES $M4FILES"
   fi
   if [ "" != "$FILES" ] ; then
      (cd $DIR && perl $bindir/depend.pl . '-' $FILES)

      #
      # If Makefile.depend does not exist, then create it
      #

      if [ ! -r $DIR/Makefile.depend ] ; then
         echo "   creating "$DIR/Makefile.depend
         mv -f $DIR/Makefile.depend.tmp $DIR/Makefile.depend

      #
      # Otherwise, copy if the two files are not the same.  Remove the CVS
      # portions of the header to ignore changes in date/revision/modified.
      #

      else
         if $COMP $DIR/Makefile.depend.tmp $DIR/Makefile.depend ; then
            rm -f $DIR/Makefile.depend.tmp
         else
            echo "   updating "$DIR/Makefile.depend
            mv -f $DIR/Makefile.depend.tmp $DIR/Makefile.depend
         fi
      fi
   fi
done

exit 0
